package com.tevintao.marketingsystem.task;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.TaskLogicService;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.HookWindowManagerGlobal;
import com.tevintao.marketingsystem.xposted.WxTaskManger;

import org.json.JSONObject;


/**
 * 接受别人发送的好友
 */
public class AcceptAllFriendTaskProcess extends SetTaskExtra {
    private static AcceptAllFriendTaskProcess ourInstance = new AcceptAllFriendTaskProcess();

    public static AcceptAllFriendTaskProcess getInstance() {
        return ourInstance;
    }

    private AcceptAllFriendTaskProcess() {
    }

    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {
        if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {
            boolean contacts = WxUiViewConfig.inTab(v, WxUiViewConfig.TAB_CONTACTS);
            if (contacts) {
                WxUiViewConfig.printViewGroup((ViewGroup) v);
                View newItem = v.findViewById(WxUiViewConfig.ID_ACCEPT_FRIEND_LIST_ITEM);
                if (null != newItem) {
                    ViewOperation.clickView(newItem);
                } else {
                    View newFriend = v.findViewById(WxUiViewConfig.ID_NEW_FRIEND_LIST_ITEM);
                    if (null != newFriend) {
                        ViewOperation.clickView(newFriend);
                    }
                }
            }

        } else if (WxUiViewConfig.VIEW_NEW_FRIEND_LIST.equals(contextName)) {
            boolean canNext = false;
            View friendList = v.findViewById(2131625708);
            if (friendList instanceof ListView) {
                ViewGroup vg = (ViewGroup) friendList;
                for (int i = 0; i < vg.getChildCount(); i++) {
                    View accept = vg.findViewById(WxUiViewConfig.ID_NEW_FRIEND_LIST_ITEM_ACCEPT);
                    if (null != accept && accept.getVisibility() == View.VISIBLE) {
                        ViewOperation.clickView(accept);
                        LogManager.getInstance().logInfo("接受第1个好友请求");
                        canNext = true;
                        break;
                    }
                }
            }
            if (!canNext) {
                taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "添加完毕");
            }
        } else if (WxUiViewConfig.VIEW_CONTACT_INFO_UI.equals(contextName)) {
            View send = v.findViewById(WxUiViewConfig.ID_CONTACTS_INFO_SEND);
            if (send != null && send.getVisibility() == View.VISIBLE) {
                TaskLogicService.sendKeyEvent(v.getContext(), TaskLogicService.INPUT_TYPE_BACK);
            }
            View sayHi = v.findViewById(WxUiViewConfig.ID_CONTACTS_INFO_SAY_HI);
            if (sayHi != null && sayHi.getVisibility() == View.VISIBLE) {
                ViewOperation.clickView(sayHi);
            }
            View addContacts = v.findViewById(WxUiViewConfig.ID_CONTACTS_INFO_ADD);
            if (addContacts != null && addContacts.getVisibility() == View.VISIBLE) {
                ViewOperation.clickView(addContacts);
            }
        } else if (WxUiViewConfig.VIEW_SEARCH_FRIEND_SAY_HI.equals(contextName)) {
            String sayHiMsg = "你好";
            if (!TextUtils.isEmpty(sayHiMsg)) {
                View sayContent = v.findViewById(WxUiViewConfig.KEY_SEARCH_FRIEND_SAY_HI_TEXT_VIEW);
                if (null != sayContent && sayContent instanceof TextView) {
                    ViewOperation.setText(sayContent, sayHiMsg);
                }
            }
            View sendView = v.findViewById(WxUiViewConfig.KEY_SEARCH_FRIEND_SAY_HI_SEND);
            if (null != sendView) {
                ViewOperation.clickView(sendView);
                TaskLogicService.sendKeyEvent(v.getContext(), TaskLogicService.INPUT_TYPE_BACK);
            }
        }
        return false;
    }
}
